import '@renderer/assets/main.css'

import { createApp } from 'vue'
import Aura from '@primevue/themes/aura'
import PrimeVue from 'primevue/config'
import 'primeicons/primeicons.css'

// Vuetify
import 'vuetify/styles'
import { createVuetify } from 'vuetify'
import { Ripple } from 'vuetify/directives'
import ConfirmationService from 'primevue/confirmationservice'
import ToastService from 'primevue/toastservice'

export const create = (App) => {
  const app = createApp(App)

  app.use(
    createVuetify({
      theme: {
        defaultTheme: 'dark'
      },
      directives: { Ripple }
    })
  )

  app.use(ConfirmationService)
  app.use(ToastService)
  app.use(PrimeVue, {
    ripple: true,
    theme: {
      preset: Aura,
      options: {
        prefix: 'p'
      }
    }
  })

  app.mount('#app')
  return app
}

export function getUrlParam(url, name) {
  return decodeURIComponent(new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(url)?.[1].replace(/\+/g, '%20') || '') || null
}

export function request(event, ...arg) {
  const { send, once } = window.electron.ipcRenderer
  return new Promise((resolve) => {
    send(event, ...arg)
    once(event + ':return', function (event, res) {
      resolve(res)
    })
  })
}

export const getSizeByDir = (path) => {
  const fs = window.require('fs')
  const { sumBy } = window.require('lodash')
  const list = fs.readdirSync(path, { withFileTypes: true, recursive: true })
  let size = sumBy(
    list.map((_) => fs.statSync(`${_.parentPath}\\${_.name}`)),
    'size'
  )
  // console.log(size)
  return size
}
